<template>
  <div class="app-container">
    <el-card>
      <div slot="header">评论列表</div>
      <el-form :inline="true">
        <div class="filter-container">
          <el-select v-model="listQuery.sort" placeholder="排序" clearable style="width: 90px" class="filter-item">
            <el-option v-for="item in ordersort" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-input v-model="listQuery.keyword" placeholder="书籍ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.status" placeholder="全部" clearable style="width: 90px" class="filter-item">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            搜索
          </el-button>
          <!-- <el-button class="filter-item" style="margin-left: 0px;" type="success" icon="el-icon-edit" @click="handleCreate">
            新增
          </el-button> -->
        </div>
      </el-form>
      <el-table ref="multipleTable" v-loading="loading" tooltip-effect="dark" :data="list" style="width: 100%;" stripe @selection-change="handleSelectionChange">
        <el-table-column align="left" type="selection" width="50" />
        <el-table-column align="left" label="ID" width="60">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="书名" width="80">
          <template slot-scope="scope">
            {{ scope.row.book.name }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="用户">
          <template slot-scope="scope">
            {{ scope.row.user.nickname }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="评论内容" width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.content }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="模块标识" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.book.module_id == 1" type="success" size="mini">小说</el-tag>
            <el-tag v-if="scope.row.book.module_id == 2" type="warning" size="mini">漫画</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="70">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 2" type="success" size="mini">审核通过</el-tag>
            <el-tag v-if="scope.row.status == 1" type="warning" size="mini">待审核</el-tag>
            <el-tag v-else-if="scope.row.status == 0" type="danger" size="mini">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <!-- <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope)">编辑</el-button> -->
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button class="el-pl" plain size="mini" style="margin-left: 0px;" type="success" @click="handleBatchDisable(2)">
        批量审核
      </el-button>
      <el-button class="el-pl" plain size="mini" style="margin-left: 0px;" type="warning" @click="handleBatchDisable(1)">
        批量禁用
      </el-button>
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="10"
        :current-page="listQuery.page"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <!-- <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑书籍':'新增书籍'">
      <el-form v-loading="loadingForm" :model="data" label-width="140px">
        <el-form-item label="书籍ID">
          <el-input v-model="data.book_id" placeholder="请输入书籍ID" />
        </el-form-item>
        <el-form-item label="章节名">
          <el-input v-model="data.name" placeholder="请输入章节名称" />
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input v-model="data.sort" placeholder="这里是第几章" />
        </el-form-item>
        <el-form-item label="章节描述">
          <el-input
            v-model="data.description"
            type="textarea"
            :rows="3"
            placeholder="请输入章节描述"
          />
        </el-form-item>
        <el-form-item label="章节内容">
          <vue-tinymce
            v-model="data.content"
            :setting="setting"
          />
        </el-form-item>
        <el-form-item label="漫画图片">
          <el-upload
            ref="upload"
            class="upload-demo"
            :action="upAction"
            :headers="upHeaders"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
            :limit="9"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" size="small" @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" size="small" @click="confirmData">确 认</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getDatas, addData, deleteData, updateData, batchDisable, getBookModulesList } from '@/api/comments'
import { getCategoryOptions } from '../../api/book_type'
import { getToken } from '../../utils/auth'

const defaultData = {
  id: '',
  category_id: '',
  title: ''
}

export default {
  data() {
    return {
      setting: {
        menubar: false,
        toolbar: 'undo redo | fullscreen | formatselect alignleft aligncenter alignright alignjustify | link unlink | numlist bullist | image media table | fontselect fontsizeselect forecolor backcolor | bold italic underline strikethrough | indent outdent | superscript subscript | removeformat |',
        toolbar_drawer: 'sliding',
        quickbars_selection_toolbar: 'removeformat | bold italic underline strikethrough | fontsizeselect forecolor backcolor',
        plugins: 'link image media table lists fullscreen quickbars',
        language: 'zh_CN', // 本地化设置
        language_url: 'tinymce/langs/zh-Hans.js',
        height: 350
      },
      modules: [],
      loading: false,
      loadingForm: false,
      list: [],
      total: 0,
      ordersort: [
        {
          value: '1',
          label: '顺序'
        },
        {
          value: '2',
          label: '倒序'
        }
      ],
      statusOptions: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '1',
          label: '待审核'
        }, {
          value: '2',
          label: '通过审核'
        }
      ],
      categoryOptions: [],
      listQuery: {
        page: 1,
        limit: 10,
        keyword: '',
        type: '',
        order: '',
        type_id: []
      },
      fileList: [],
      fileVideoList: [],
      batch: {
        selection: [],
        status: 1
      },
      upAction: process.env.VUE_APP_BASE_API + '/upload',
      upHeaders: {
        Authorization: getToken()
      },
      data: Object.assign({}, defaultData),
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false
    }
  },
  created() {
    this.getList()
    // this.handBookModules()
    // this.handleCategoryOptions()
  },
  methods: {
    moduleschange(value) {
      this.modulequery.model_id = value
      this.handleCategoryOptions()
    },
    typechange(value) {
      console.log(value)
      this.listQuery.type_id = value
    },
    async getList() {
      this.loading = true
      const res = await getDatas(this.listQuery)
      this.list = res.data.data
      console.log(this.list)
      this.total = res.data.total
      this.listQuery.page = res.data.current_page
      this.loading = false
    },
    async handleBatchDisable(status) {
      if (this.batch.selection.length <= 0) {
        this.$message({
          type: 'error',
          message: '至少选中一个选项'
        })
        return
      }
      this.batch.status = status
      await batchDisable(this.batch)
      this.getList()
    },
    async handleCategoryOptions() {
      const res = await getCategoryOptions(this.modulequery)
      if (res.code === 200) {
        console.log(res.data)
        this.categoryOptions = res.data
      }
    },
    async handBookModules() {
      const res = await getBookModulesList()
      if (res.code === 200) {
        console.log(res.data)
        this.modules = res.data
      }
    },
    handleSelectionChange(obj) {
      this.batch.selection = []
      obj.forEach(item => {
        this.batch.selection.push(item.id)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.fileList = []
      this.fileVideoList = []
      this.data = Object.assign({}, defaultData)
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleRemove(file, fileList) {
      // 移除文件时，要重新给fileList赋值
      const arr = []
      const images = []
      for (let i = 0; i < this.fileList.length; i++) {
        if (this.fileList[i].uid !== file.uid) {
          arr.push(this.fileList[i])
          images.push(this.fileList[i].img_url)
        }
      }
      this.fileList = arr
      this.images = images
      this.data.images = images
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.checkStrictly = true
      this.data = deepClone(scope.row)
      this.data.book_id = scope.row.book.id
      this.images = this.data.images
      this.fileList = []
      for (const key in this.data.images_list) {
        this.fileList.push({
          name: '图片' + key,
          img_url: this.data.images[key],
          url: this.data.images_list[key]
        })
      }
      this.fileVideoList = []
      if (this.data.thumb2) {
        this.fileVideoList.push({
          name: '视频',
          url: this.data.thumb2
        })
      }
      this.$nextTick(() => {
        this.checkStrictly = false
      })
    },
    handleSuccess(res, file, fileList) {
      this.loadingForm = false
      if (res.code === 200) {
        this.images.push(res.data.img_url)
        this.data.images = this.images
        this.fileList.push({
          name: res.data.name,
          img_url: res.data.img_url,
          url: res.data.url
        })
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
    },
    beforeUpload(file) {
      if (file.type !== 'image/jpeg' && file.type !== 'image/jpg' && file.type !== 'image/png' && file.type !== 'image/gif') {
        this.$message({
          type: 'error',
          message: '图片格式不正确'
        })
        return false
      }
      this.loadingForm = true
      return true
    },
    handleDelete({ $index, row }) {
      this.$confirm('确定禁用该条数据?', '警告', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      })
        .then(async() => {
          await deleteData(row.id)
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmData() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        await updateData(this.data.id, this.data)
      } else {
        await addData(this.data)
      }
      this.getList()
      this.dialogVisible = false
      this.$message({
        type: 'success',
        message: '保存成功'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
